<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
//error_reporting(E_ALL);
/**
 *
 * @author RAMIANDRISOA Domoina
 * @tel 261 34 13 306 32
 * @email domonaalivelo@gmail.com
 */

class Clinic extends CI_Controller {

	private $per_page = 5;
	private $uri_segment = 3;
	
	function __construct()
	    {
		//parent::__construct();
		//$this->load->helper(array('form', 'url'));
		//$this->load->library('session');
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('session');
	    }

	function index()
	{
		$this->load->view('clinic/vue_profilClinic');
	}

	function ajoutC1 (){
                $this->form_validation->set_rules('email','"Adresse email"','trim|required|xss_clean|valid_email|is_unique[COMPTE.EMAIL]');
                $this->form_validation->set_rules('password_clinic','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('stdr/vue_accueil');
                } else {
			$email = $this->input->post('email');
			/*$id_patient = $this->MDn->verifEmailComptePatient($email);
			$id_clinique = $this->MDn->verifEmailCompteClinique($email);*/
			/*if($id_patient == "" && $id_clinique == ""){*/
					$data = array(
					'email' => $email ,
					'mdp' => $this->input->post('password_clinic')
                                                );
				$this->MDn->insertC1($data);
				echo "<script>alert('inscription r&eacute;ussie')</script>";
				$result = $this->MDn->getInfoClinic($email);
				foreach ($result as $key => $list){
					$id_clinique = intval($list['id_clinique']);				
					$this->envoiMailClinic($email, $id_clinique);
				}
			/*}else{
				echo "<script>alert('email déjà utilisée pour une inscription!')</script>";
			}*/
                        $this->load->view('stdr/vue_accueil');
		}
			
	}

	
	function ajoutGalerieImage (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getLogoImagePple($id_clinique);
		$data['result1']= $this->MDn->getGalPhoto($id_clinique);
		$data['result2']= $this->MDn->getSoinPhoto($id_clinique);
		$this->load->view('pagedn/ajoutGalerieImage', $data);
	}
	
	function devisRecus (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getAllDevisRecus($id_clinique);
		$this->load->view('pagedn/devisRecus', $data);
	}

	function msgRecus (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getAllMsgClinicRecus($id_clinique,1) ;
		$this->load->view('pagedn/msgRecus', $data);
	}
	
	function commentaireRecus (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getAllCommentairesClinicRecus($id_clinique) ;
		$this->load->view('pagedn/commentaireRecus', $data);
	}
	
	//debut carroussel
	function carroussel (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getGalPhoto($id_clinique);
		$data['resultF']= $this->MDn->getFirstImgGalPhoto($id_clinique);
		$data['resultS']= $this->MDn->getSecondImgGalPhoto($id_clinique);
		$this->load->vars($data);
		//$this->load->view('pagedn/carroussel');
		$this->load->view('pagedn/carroussel_reser');
	}
	//fin carroussel
	
	function updateC1 (){
		$id_clinique = $this->input->get('id');
		//echo "mijakely".$id_clinique;
		if (isset($_POST['submit']))
		{
			$iCnt = $this->input->post('iCnt');
			$isInfoUpdate = 1;
			$marque_implant1 = "";$marque_implant2 = "";$marque_implant3 = "";
			//echo "domoina".$iCnt;
			if($iCnt==0){
				$marque_implant1=$this->input->post('marque_implant1');
			}
			if($iCnt==1){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
			}
			else if($iCnt==2){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$marque_implant2=$valueexpanne2;
			}
			else if($iCnt==3){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$marque_implant1=$valueexpanne1;
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$marque_implant2=$valueexpanne2;
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$marque_implant3=$valueexpanne3;
			}
			$data = array(
				'id_clinique'=> $id_clinique, 
				/*'logo' => $_FILES["logo"]['name'],*/
				'nom_clinique'=>$this->input->post('nom_clinique'),
				'id_ville'=>$this->input->post('ville'),
				'id_pays'=>$this->input->post('pays'),
				'adresse'=>$this->input->post('adresse'),
				'code_postal'=>$this->input->post('code_postal'),
				/*'photo1' => $_FILES["photo1"]['name'],
				'photo2' => $_FILES["photo2"]['name'],
				'photo3' => $_FILES["photo3"]['name'],
				'photo4' => $_FILES["photo4"]['name'],
				'photo5' => $_FILES["photo5"]['name'],
               			'photo6' => $_FILES["photo6"]['name'],
				'photo7' => $_FILES["photo7"]['name'],
				'photo8' => $_FILES["photo8"]['name'],
				'photo9' => $_FILES["photo9"]['name'],
				'photo10' => $_FILES["photo10"]['name'],*/
				'prothese'=>$this->input->post('prothese'),
				'scanner'=>$this->input->post('scanner'),
				'origine_materiaux1'=>$this->input->post('origine_materiaux1'),
				'origine_materiaux2'=>$this->input->post('origine_materiaux2'),
				'origine_materiaux3'=>$this->input->post('origine_materiaux3'),
				'fauteuil'=>$this->input->post('fauteuil'),
				'surface'=>$this->input->post('surface'),
				'marque_implant1'=>$marque_implant1,
				'marque_implant2'=>$marque_implant2,     
				'marque_implant3'=>$marque_implant3,
				'isInfoUpdate'=>$isInfoUpdate
			);
			$this->MDn->updateC1($data);
			$this->load->library('upload');  
			//$id_clinique = $this->input->post('id_clinique');
                        for($i=1; $i<=10; $i++){
				$filename = 'photo'.$i;
				if (!empty($_FILES[$filename]['name']))
				{
				    // Specify configuration for File          
				    $config['upload_path'] = 'upload/';
				    $config['allowed_types'] = 'gif|jpg|png';
				    $config['max_size'] = '200000';
				    $config['max_width']  = '800';
				    $config['max_height']  = '600';  
				    // Initialize config for File 
				    $this->upload->initialize($config);
				    // Upload file 
				    if ($this->upload->do_upload($filename))
				    {
				        $data = $this->upload->data();
				        $url = $config['upload_path']."".$_FILES[$filename]['name'];
				    }
				    else
				    {
				        echo $this->upload->display_errors();
				    }
				}
	       	}			
			$data['result']= $this->MDn->getInfoClinicId($id_clinique);
			echo "<script>alert('Enregistré')</script>";
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$data['origine_materiaux3']= $this->MDn->getAllPays();
			$data['origine_materiaux2']= $this->MDn->getAllPays();
			$data['origine_materiaux1']= $this->MDn->getAllPays();
			$this->load->vars($data);
			$this->load->view('pagedn/header');
            		$this->load->view('pagedn/updateC1affiche');
			$this->load->view('pagedn/footer');
		}
		else {
			$data['result']= $this->MDn->getInfoClinicId($id_clinique);
			$data['pays']= $this->MDn->getAllPays();
			$data['ville']= $this->MDn->getAllVille(1);
			$data['origine_materiaux3']= $this->MDn->getAllPays();
			$data['origine_materiaux2']= $this->MDn->getAllPays();
			$data['origine_materiaux1']= $this->MDn->getAllPays();
			
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/updateC1', $data);
			$this->load->view('pagedn/footer');
                        /*$this->load->view('clinique/vue_updateClinique',$data);*/
		}	
	}

	function updateC1affiche (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getInfoClinicId($id_clinique);
		$data['pays']= $this->MDn->getAllPays();
		$data['ville']= $this->MDn->getAllVille(1);
		$data['origine_materiaux3']= $this->MDn->getAllPays();
		$data['origine_materiaux2']= $this->MDn->getAllPays();
		$data['origine_materiaux1']= $this->MDn->getAllPays();
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/updateC1affiche', $data);
		$this->load->view('pagedn/footer');
			
	}

	function ajoutDentiste (){
		
		if (isset($_POST['submit']))
		{				
			$id_clinique = $this->input->get('id');
			$filename = 'photo_dentiste';
			$iCnt = $this->input->post('iCnt');
			$exp1 = "";$exp2 = "";$exp3 = "";$exp4 = "";$exp5 = "";$exp6 = "";
			$annexp1 = "";$annexp2 = "";$annexp3 = "";$annexp4 = "";$annexp5 = "";$annexp6 = "";
			$ann2exp1 = "";$ann2exp2 = "";$ann2exp3 = "";$ann2exp4 = "";$ann2exp5 = "";$ann2exp6 = "";
			if($iCnt==0){
				$annexp1=$this->input->post('annee_exp1');
				$ann2exp1=$this->input->post('annee2_exp1');
				$exp1=$this->input->post('exp1');
			}
			if($iCnt==1){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
			}
			else if($iCnt==2){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$tab2=explode("_/",$valueexpanne2);
				$annexp2=$tab2[0];
				$ann2exp2=$tab2[1];
				$exp2=$tab2[2];
			}
			else if($iCnt==3){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$tab2=explode("_/",$valueexpanne2);
				$annexp2=$tab2[0];
				$ann2exp2=$tab2[1];
				$exp2=$tab2[2];
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$tab3=explode("_/",$valueexpanne3);
				$annexp3=$tab3[0];
				$ann2exp3=$tab3[1];
				$exp3=$tab3[2];
			}
			else if($iCnt==4){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$tab2=explode("_/",$valueexpanne2);
				$annexp2=$tab2[0];
				$ann2exp2=$tab2[1];
				$exp2=$tab2[2];
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$tab3=explode("_/",$valueexpanne3);
				$annexp3=$tab3[0];
				$ann2exp3=$tab3[1];
				$exp3=$tab3[2];
				$valueexpanne4 = $this->input->post('valueexpanne4');
				$tab4=explode("_/",$valueexpanne4);
				$annexp4=$tab4[0];
				$ann2exp4=$tab4[1];
				$exp4=$tab4[2];
			}
			else if($iCnt==5){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$tab2=explode("_/",$valueexpanne2);
				$annexp2=$tab2[0];
				$ann2exp2=$tab2[1];
				$exp2=$tab2[2];
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$tab3=explode("_/",$valueexpanne3);
				$annexp3=$tab3[0];
				$ann2exp3=$tab3[1];
				$exp3=$tab3[2];
				$valueexpanne4 = $this->input->post('valueexpanne4');
				$tab4=explode("_/",$valueexpanne4);
				$annexp4=$tab4[0];
				$ann2exp4=$tab4[1];
				$exp4=$tab4[2];
				$valueexpanne5 = $this->input->post('valueexpanne5');
				$tab5=explode("_/",$valueexpanne5);
				$annexp5=$tab5[0];
				$ann2exp5=$tab5[1];
				$exp5=$tab5[2];
			}
			else if($iCnt==6){
				$valueexpanne1 = $this->input->post('valueexpanne1');
				$tab=explode("_/",$valueexpanne1);
				$annexp1=$tab[0];
				$ann2exp1=$tab[1];
				$exp1=$tab[2];
				$valueexpanne2 = $this->input->post('valueexpanne2');
				$tab2=explode("_/",$valueexpanne2);
				$annexp2=$tab2[0];
				$ann2exp2=$tab2[1];
				$exp2=$tab2[2];
				$valueexpanne3 = $this->input->post('valueexpanne3');
				$tab3=explode("_/",$valueexpanne3);
				$annexp3=$tab3[0];
				$ann2exp3=$tab3[1];
				$exp3=$tab3[2];
				$valueexpanne4 = $this->input->post('valueexpanne4');
				$tab4=explode("_/",$valueexpanne4);
				$annexp4=$tab4[0];
				$ann2exp4=$tab4[1];
				$exp4=$tab4[2];
				$valueexpanne5 = $this->input->post('valueexpanne5');
				$tab5=explode("_/",$valueexpanne5);
				$annexp5=$tab5[0];
				$ann2exp5=$tab5[1];
				$exp5=$tab5[2];
				$valueexpanne6 = $this->input->post('valueexpanne6');
				$tab6=explode("_/",$valueexpanne6);
				$annexp6=$tab6[0];
				$ann2exp6=$tab6[1];
				$exp6=$tab6[2];
			}
			$iCL = $this->input->post('iCL');
			$langue2 = "";
			$langue3 = "";
			if ($iCL == 1){
				$langue2 = $this->input->post('langue1');
			}
			else if($iCL == 2){
				$langue2 = $this->input->post('langue1');
				$langue3 = $this->input->post('langue2');
			}
			$data = array(
				'id_clinique' => $id_clinique,
				'photo_dentiste' => $this->input->post('photo_dentistehidden'),
				'nom_dentiste'=>$this->input->post('nom_dentiste'),
				'fonction_dentiste'=>$this->input->post('fonction_dentiste'),
				'langue1'=>$this->input->post('langue'),
				'langue2'=> $langue2,
				'langue3'=> $langue3
			);
			$this->MDn->insertDentiste($data);
			$id_dentiste = $this->MDn->recupLastInsertDentiste($id_clinique);
			/*$data1 = array(
				'id_clinique' => $id_clinique,
				'id_dentiste' => $id_dentiste,
				'exp1'=>$exp1,
				'exp2'=>$exp2,
				'exp3'=>$exp3,
				'exp4'=>$exp4,
				'exp5'=>$exp5,
				'exp6'=>$exp6,
				'annee_exp1'=>$annexp1,
				'annee_exp2'=>$annexp2,
				'annee_exp3'=>$annexp3,
				'annee_exp4'=>$annexp4,
				'annee_exp5'=>$annexp5,
				'annee_exp6'=>$annexp6,
				'annee2_exp1'=>$ann2exp1,
				'annee2_exp2'=>$ann2exp2,
				'annee2_exp3'=>$ann2exp3,
				'annee2_exp4'=>$ann2exp4,
				'annee2_exp5'=>$ann2exp5,
				'annee2_exp6'=>$ann2exp6
			);
			*/
			
			for ($i=1; $i<=6; $i++){
				$annee1 = ${'annexp'.$i}; 
				$annee2 = ${'ann2exp'.$i};
				$exp = ${'exp'.$i};
				if ($exp!=""){
					$data1 = array(
						'annee1' => $annee1,
						'exp' => $exp,
						'annee2'=>$annee2,
						'id_dentiste'=>$id_dentiste
					);
					$this->MDn->insertExp($data1);
				}
			}
			
			//$this->MDn->insertExpDentiste($data1);
			$data['result']= $this->MDn->getAllDentiste($id_clinique);
			//$data['resultEP']= $this->MDn->getAllExpProfDentiste($id_clinique);
			//$data['isc'] = 1;
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutDentiste');
		}
		else {
			//$data['isc'] = 0;
			$id_clinique = $this->input->get('id');
			$data['result']= $this->MDn->getAllDentiste($id_clinique);
			$data['resultEP']= $this->MDn->getAllExpProfDentiste($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutDentiste');
		}	
		
	}
	
	function ajoutConditionEtGarantie(){
		if (isset($_POST['submit']))
		{				
			$id_clinique = $this->input->post('id_clinique');
			$data = array(
				'id_clinique' => $id_clinique,
				'accompagnatrice'=>$this->input->post('accompagnatrice'),
				'implant'=>$this->input->post('implant'),
				'prothese'=>$this->input->post('prothese'),
				'inlay_Onlay'=>$this->input->post('inlay_Onlay'),
				'passeportdentaire'=>$this->input->post('passeportdentaire')
			);
			if($this->MDn->verifCG($id_clinique)==1){
				$this->MDn->updateConditionEtGarantie($data);
			}
			else{
				$this->MDn->insertConditionEtGarantie($data);
			}
			echo "<script>alert('enregistré')</script>";
			$data['result']= $this->MDn->getConditionEtGarantie($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutConditionEtGarantie');
		}
		else {
			$id_clinique = $this->input->get('id');
			$data['result']= $this->MDn->getConditionEtGarantie($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutConditionEtGarantie');
		}	
	}
	
	function ajoutcg(){
		if (isset($_POST['submit']))
		{				
			$id_clinique = $this->input->post('id_clinique');
			$data = array(
				'id_clinique' => $id_clinique,
				'accompagnatrice'=>$this->input->post('accompagnatrice'),
				'implant'=>$this->input->post('implant'),
				'prothese'=>$this->input->post('prothese'),
				'inlay_Onlay'=>$this->input->post('inlay_Onlay'),
				'passeportdentaire'=>$this->input->post('passeportdentaire')
			);
			if($this->MDn->verifCG($id_clinique)==1){
				$this->MDn->updateConditionEtGarantie($data);
			}
			else{
				$this->MDn->insertConditionEtGarantie($data);
			}
			echo "<script>alert('enregistré')</script>";
			$data['result']= $this->MDn->getConditionEtGarantie($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutConditionEtGarantie');
		}
		else {
			$id_clinique = $this->input->get('id');
			$data['result']= $this->MDn->getConditionEtGarantie($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutConditionEtGarantie');
		}	
	}
	
	function detailDentiste (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getAllDentiste($id_clinique);
		$data['resultEP']= $this->MDn->getAllExpProfDentiste($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/detailDentiste');
		
	}
	
	function afficheIframe (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getGoogleMap($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/afficheIframe');
		
	}
	
	function traitementPrix (){
		$id_clinique = $this->input->get('id');
		$data['result']= $this->MDn->getPrixSoinsMisAvant($id_clinique);
		$data['prix1']= $this->MDn->getPrixSoins1($id_clinique);
		$data['prix2']= $this->MDn->getPrixSoins2($id_clinique);
		$data['prix3']= $this->MDn->getPrixSoins3($id_clinique);
		$data['prix4']= $this->MDn->getPrixSoins4($id_clinique);
		$data['prix5']= $this->MDn->getPrixSoins5($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/traitementPrix');
		
	}

	function supprDentiste (){
		$id_dentiste = intval($this->input->get('id'));
		$id_clinique = intval($this->input->get('idc'));
		$this->MDn->deleteDentiste($id_dentiste);
		//echo '<script>alert("Supprimé"); document.location = "ajoutDentiste?id='.$id_clinique.'";</script>';
		echo '<script>alert("Supprimé");</script>';
		$data['result']= $this->MDn->getAllDentiste($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/ajoutDentiste');
	}
	
	function supprDemandeDevisR (){
		$id_devisRecus = intval($this->input->get('id'));
		$this->MDn->deleteDemandeDevisR($id_devisRecus);
		echo '<script>alert("Supprimé");</script>';
		$data['result']= $this->MDn->getAllDevisRecus($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/devisRecus');
	}
	
	function supprMessageClinic (){
		$id_message = intval($this->input->get('id'));
		$this->MDn->deleteMessageClinic($id_message);
		//echo '<script>alert("Supprimé");</script>';
		$data['result']= $this->MDn->getAllMsgClinicRecus($id_clinique,1) ;
		$this->load->vars($data);
		$this->load->view('pagedn/msgRecus');
	} 

	function supprCommentaireClinic (){
		$id_commentaire = intval($this->input->get('id'));
		$id_clinique = intval($this->input->get('id_clinique'));
		$this->MDn->deleteCommentaireClinic($id_commentaire);
		//echo '<script>alert("Supprimé");</script>';
		$data['result']= $this->MDn->getAllCommentairesClinicRecus($id_clinique) ;
		$this->load->vars($data);
		$this->load->view('pagedn/commentaireRecus');
	}
	
	function validCommentaireClinic (){
		$id_commentaire = intval($this->input->get('id'));
		$id_clinique = intval($this->input->get('id_clinique'));
		$this->MDn->updateCommentaireClinic($id_commentaire);
		$data['result']= $this->MDn->getAllCommentairesClinicRecus($id_clinique) ;
		$this->load->vars($data);
		$this->load->view('pagedn/commentaireRecus');
	}
	
	function listclinicOld (){
		$data['result']= $this->MDn->getAllClinic();
		$data['pays']= $this->MDn->getAllPays();
		$data['ville']= $this->MDn->getAllVille(1);
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/listclinic', $data);
		$this->load->view('pagedn/footer');	
	}
	
	function listclinic (){
		$this->load->library("pagination");
		$config = array();
		$config["base_url"] = base_url() . "index.php/clinic/listclinic";
		$config["total_rows"] = $this->MDn->record_count_getAllClinicFetch();
		$config["per_page"] = $this->per_page;
		$config["uri_segment"] = $this->uri_segment;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$data['result']= $this->MDn->getAllClinicFetch($config["per_page"], $page);
		$data["links"] = $this->pagination->create_links();
                /*$this->load->view('pagedn/header');
		$this->load->view('pagedn/listclinic', $data);
                $this->load->view('pagedn/footer');*/
                $this->load->view('clinic/vue_listclinic',$data);
	}

	function modifEmail() {
		$email = $this->input->post('email');
		$id_clinic = $this->input->post('id_clinic');
		if($email !="" && $id_clinic !=""){
			$result = $this->MDn->updateEmailClinic($email, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifLocalite() {
		$adresse = $this->input->post('adresse');
		$id_pays = $this->input->post('pays');
		$id_ville = $this->input->post('ville');
		$code_postal = $this->input->post('code_postal');
		$map = $this->input->post('map');
		$id_clinic = $this->input->post('id_clinic');
		if($adresse !="" && $code_postal !=""){
			$result = $this->MDn->updateLocaliteClinic($adresse, $code_postal, $map, $id_pays, $id_ville, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifMateriel() {
		$marque1 = $this->input->post('marque1');
		$marque2 = $this->input->post('marque2');
		$marque3 = $this->input->post('marque3');
		$origine1 = $this->input->post('origine1');
		$origine2 = $this->input->post('origine2');
		$origine3 = $this->input->post('origine3');
		$id_clinic = $this->input->post('id_clinic');
		if($id_clinic!=0){
			if($marque1=="" && $marque2 != "" && $marque3 !=""){
				$marque1 = $marque2;
				$marque2 = $marque3;
				$marque3 ="";
			}
			else if($marque1=="" && $marque2 == "" && $marque3 !=""){
				$marque1 = $marque3;
				$marque2 = "";
				$marque3 ="";
			}
			else if($marque1!="" && $marque2 == "" && $marque3 !=""){
				$marque2 = $marque3;
				$marque3 ="";
			}
			else if($marque1=="" && $marque2 = !"" && $marque3 ==""){
				$marque1 = $marque2;
				$marque2 = "";
			}
			
			if($origine1==0 && $origine2 != 0 && $origine3 !=0){
				$origine1 = $origine2;
				$origine2 = $origine3;
				$origine3 =0;
			}
			else if($origine1==0 && $origine2 == 0 && $origine3 !=0){
				$origine1 = $origine3;
				$origine2 = 0;
				$origine3 =0;
			}
			else if($origine1!=0 && $origine2 == 0 && $origine3 !=0){
				$origine2 = $origine3;
				$origine3 =0;
			}
			else if($origine1==0 && $origine2 == !0 && $origine3 ==0){
				$origine1 = $origine2;
				$origine2 = 0;
			}
			$result = $this->MDn->updateMaterielClinic($marque1, $marque2, $marque3, $origine1, $origine2, $origine3, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	
	function modifMateriel1() {
		$marque1 = $this->input->post('marque1');
		$marque2 = $this->input->post('marque2');
		$marque3 = $this->input->post('marque3');
		if($id_clinic!=0){
			if($marque1=="" && $marque2 != "" && $marque3 !=""){
				$marque1 = $marque2;
				$marque2 = $marque3;
				$marque3 ="";
			}
			else if($marque1=="" && $marque2 == "" && $marque3 !=""){
				$marque1 = $marque3;
				$marque2 = "";
				$marque3 ="";
			}
			else if($marque1!="" && $marque2 == "" && $marque3 !=""){
				$marque2 = $marque3;
				$marque3 ="";
			}
			else if($marque1=="" && $marque2 = !"" && $marque3 ==""){
				$marque1 = $marque2;
				$marque2 = "";
			}
			$result = $this->MDn->updateMaterielClinic1($marque1, $marque2, $marque3, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
/*
	function downloadImage (){
		$this->load->helper('download');
		$file = $this->input->get('file');
		str_replace(" ", "", $file);
		$url = base_url();
		//$data = file_get_contents("http://aihdownload.adobe.com/bin/live/AdobeFlashPlayerInstaller_14_ltrosxd_aaa_aih.dmg");
		$data = file_get_contents($url.'upload/demandedevis/'.$file);
		//echo $url.'upload/demandedevis/'.$file;
		//$data = file_get_contents("./upload/demandedevis/fd73197917a2.jpg"); // Read the file's contents
		$name = 'myphoto.jpg';
		force_download($file, $data);
		exit();
	}
*/	
		
	function downloadImage(){
		$this->load->helper('download');
		$file = $this->input->get('file');
		str_replace(" ", "", $file);
		$url = base_url();
		$file_name = $url.'./upload/demandedevis/'.$file;
		$file_path = "./upload/demandedevis/" ;
		header("Cache-Control: no-cache, must-revalidate");
		header("Cache-Control: post-check=0,pre-check=0");
		header("Cache-Control: max-age=0");
		header("Pragma: no-cache");
		header("Expires: 0");
		header('Content-Type: application/octet-stream'); 
		header("Content-Type: application/force-download");
		$size = filesize($filename);
		header("Content-Disposition: attachment; filename=$file");
		header("Content-Length: ".$size);
		//ob_clean(); flush(); 
		readfile($file_name);
	}
	
	
	
	function fiche(){
		
		$id_patient = $this->input->get('id');
		$data['result']= $this->MDn->getInfoPatientId($id_patient);
		if($this->MDn->verifDD($id_patient)==1){
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
		}
		$data1 = array();
		$photo1 = $this->MDn->selectImagedemandeDevis($id_patient, 1);
		$photo2 = $this->MDn->selectImagedemandeDevis($id_patient, 2);
		$photo3 = $this->MDn->selectImagedemandeDevis($id_patient, 3);
		$photo4 = $this->MDn->selectImagedemandeDevis($id_patient, 4);
		$photo5 = $this->MDn->selectImagedemandeDevis($id_patient, 5);
		if ($photo1!=""){
			$data1['photo1']= $photo1;
		}
		if ($photo2!=""){
			$data1['photo2']= $photo2;
		}
		if ($photo3!="" ){
			$data1['photo3']= $photo3;
		}
		if ($photo4!="" ){
			$data1['photo4']= $photo4;
		}
		if ($photo5!="" ){
			$data1['photo5']= $photo5;
		}
		
		if ( $photo1!="" || $photo2!="" || $photo3!="" || $photo4!="" || $photo5!=""){
			$data ['resultP'] = $data1;
		}
		else {
			$data ['resultP'] = 0;
		}
		$data2 = array();
		$langue1 = $this->MDn->selectLanguedemandeDevis($id_patient, 1);
		$langue2 = $this->MDn->selectLanguedemandeDevis($id_patient, 2);
		$langue3 = $this->MDn->selectLanguedemandeDevis($id_patient, 3);
		if ($langue1!="Selectionner"){
			$data2['langue1']= $langue1;
		}
		if ($langue2!="Selectionner"){
			$data2['langue2']= $langue2;
		}
		if ($langue3!="Selectionner" ){
			$data2['langue3']= $langue3;
		}
		if ( $langue1!="Selectionner" || $langue3!="Selectionner" || $langue2!="Selectionner"){
			$data ['resultL'] = $data2;
		}
		else {
			$data ['resultL'] = 0;
		}
		$data['pays']= $this->MDn->getAllPays();
		$data['ville']= $this->MDn->getAllVille(1);
		$this->load->vars($data);
		$this->load->view('pagedn/fiche');
	}
	
	function afficheMsgClinic(){
		$id_message = $this->input->get('id');
		$data['result']= $this->MDn->getMessageByIdMsg($id_message);
		$this->load->vars($data);
		$this->load->view('pagedn/afficheMsgClinic');
	}
	
	function commentClinic(){
		$id_clinique = $this->input->get('id_clinique');
		$data['result']= $this->MDn->getCommentaireByIdclinique($id_clinique);
		$data['id_clinique']= $id_clinique;
		$this->load->vars($data);
		$this->load->view('pagedn/commentClinic');	
	}
	
	function contactClinic(){
		if (isset($_POST['submit']))
		{				
			$id_clinique = $this->input->post('id_clinique');
			$id_patient = $this->input->post('id_patient');
			$txtobjet = $this->input->post('txtobjet');
			$content = $this->input->post('content');
			$isMsgPatient = 1;
			$date = date('Y-m-d');
			$email = $this->MDn->recupEmailPatient($id_patient);
			$this->envoiMailClinicReponseMsg($email);
			$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'objet'=>$txtobjet,
				'contenu'=>$content,
				'date_envoi'=>$date,
				'isMsgPatient'=>$isMsgPatient
			);
			if($id_patient == "" || $id_patient == 0){
				echo "<script>alert('Veuillez vous connecter en tant que patient! Merci')</script>";
			}else{
				echo "<script>alert('message envoyé')</script>";
				$this->MDn->insertMsgClinic($data);	
			}
			$data['id_patient']= $id_patient;
			$data['id_clinique']= $id_clinique;
			$this->load->vars($data);
			$this->load->view('pagedn/contactClinic');
		}
		else {
			$id_clinique = $this->input->get('id_clinique');
			$id_patient = $this->input->get('id_patient');
			$data['id_clinique']= $id_clinique;
			$data['id_patient']= $id_patient;
			$this->load->vars($data);
			$this->load->view('pagedn/contactClinic');
		}	
	}
	
	function contactPatient(){
		if (isset($_POST['submit']))
		{				
			$id_clinique = $this->input->post('id_clinique');
			$id_patient = $this->input->post('id_patient');
			$txtobjet = $this->input->post('txtobjet');
			$content = $this->input->post('content');
			$isMsgClinique = 1;
			$date = date('Y-m-d');
			$email = $this->MDn->recupEmailPatient($id_patient);
			$this->envoiMailPatientReponseMsg($email,$id_clinique);
			$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'objet'=>$txtobjet,
				'contenu'=>$content,
				'date_envoi'=>$date,
				'isMsgClinique'=>$isMsgClinique
			);
			echo "<script>alert('message envoyé')</script>";
			$this->MDn->insertMsgClinic($data);	
			$data['id_patient']= $id_patient;
			$data['id_clinique']= $id_clinique;
			$this->load->vars($data);
			$this->load->view('pagedn/contactPatient');
		}
		else {
			$id_clinique = $this->input->post('id_clinique');
			$id_patient = $this->input->post('id_patient');
			$data['id_clinique']= $id_clinique;
			$data['id_patient']= $id_patient;
			$this->load->vars($data);
			$this->load->view('pagedn/contactPatient');
		}	
	}
	
	function devisType(){
		$id_patient = $this->input->post('id_patient');
		$id_clinique = $this->input->post('id_clinique');
		$data['result']= $this->MDn->getInfoPatientId($id_patient);
		if($this->MDn->verifDD($id_patient)==1){
			$data['result1']= $this->MDn->getDemandeDevisId($id_patient);
		}
		$data['id_patient']= $id_patient;
		$data['id_clinique']= $id_clinique;
		$data['result2']= $this->MDn->getConditionEtGarantie($id_clinique);
		$data['result3']= $this->MDn->getInfoClinicId($id_clinique);
		$data1 = array();
		$photo1 = $this->MDn->selectImagedemandeDevis($id_patient, 1);
		$photo2 = $this->MDn->selectImagedemandeDevis($id_patient, 2);
		$photo3 = $this->MDn->selectImagedemandeDevis($id_patient, 3);
		$photo4 = $this->MDn->selectImagedemandeDevis($id_patient, 4);
		$photo5 = $this->MDn->selectImagedemandeDevis($id_patient, 5);
		if ($photo1!=""){
			$data1['photo1']= $photo1;
		}
		if ($photo2!=""){
			$data1['photo2']= $photo2;
		}
		if ($photo3!="" ){
			$data1['photo3']= $photo3;
		}
		if ($photo4!="" ){
			$data1['photo4']= $photo4;
		}
		if ($photo5!="" ){
			$data1['photo5']= $photo5;
		}
		
		if ( $photo1!="" || $photo2!="" || $photo3!="" || $photo4!="" || $photo5!=""){
			$data ['resultP'] = $data1;
		}
		else {
			$data ['resultP'] = 0;
		}
		$data2 = array();
		$langue1 = $this->MDn->selectLanguedemandeDevis($id_patient, 1);
		$langue2 = $this->MDn->selectLanguedemandeDevis($id_patient, 2);
		$langue3 = $this->MDn->selectLanguedemandeDevis($id_patient, 3);
		if ($langue1!="Selectionner"){
			$data2['langue1']= $langue1;
		}
		if ($langue2!="Selectionner"){
			$data2['langue2']= $langue2;
		}
		if ($langue3!="Selectionner" ){
			$data2['langue3']= $langue3;
		}
		if ( $langue1!="Selectionner" || $langue3!="Selectionner" || $langue2!="Selectionner"){
			$data ['resultL'] = $data2;
		}
		else {
			$data ['resultL'] = 0;
		}
		$data['pays']= $this->MDn->getAllPays();
		$this->load->vars($data);
		$this->load->view('pagedn/devisType');
	}
	
	function devisTypeEnvoi(){
		$id_patient = $this->input->post('id_patient');
		$id_clinique = $this->input->post('id_clinique');
		$email = $this->MDn->recupEmailPatient($id_patient);
		$this->envoiMailReponseDD($email,$id_clinique);
		$id_devisRecus = $this->MDn->recupIddevisRecus($id_clinique,$id_patient);
		$this->MDn->updateIddevisRecus(1, $id_devisRecus);
	}
	
	function devistableau(){
		$id_clinique = $this->input->post('id_clinique');
		$id_patient = $this->input->post('id_patient');
		$soin = $this->input->post('soin');
		$dent = $this->input->post('dent');
		$nbre = $this->input->post('nbre');
		$pu = $this->input->post('pu');
		$total = $this->input->post('total');
		$reftableau = 1;
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'soin'=>$soin,
				'dent'=>$dent,
				'nbdent'=>$nbre,
				'pu'=>$pu,
				'total'=>$total,
				'reftableau'=>$reftableau
			);
		if($soin != "" && $dent != "" && $nbre != "" && $pu != "" && $total != "" ){
			$this->MDn->insertDevisTableau($data);	
		}
	}
	
	function devistableau1(){
		$id_clinique = $this->input->post('id_clinique');
		$id_patient = $this->input->post('id_patient');
		$sointab = $this->input->post('sointab');
		$denttab = $this->input->post('denttab');
		$nbretab = $this->input->post('nbretab');
		$putab = $this->input->post('putab');
		$totaltab = $this->input->post('totaltab');
		$reftableau = 2;
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'soin'=>$sointab,
				'dent'=>$denttab,
				'nbdent'=>$nbretab,
				'pu'=>$putab,
				'total'=>$totaltab,
				'reftableau'=>$reftableau
			);
		if($sointab != "" && $denttab != "" && $nbretab != "" && $putab != "" && $totaltab != "" ){
			$this->MDn->insertDevisTableau($data);	
		}
	}
	
	function devistableau2(){
		$id_clinique = $this->input->post('id_clinique');
		$id_patient = $this->input->post('id_patient');
		$sointableau = $this->input->post('sointableau');
		$denttableau = $this->input->post('denttableau');
		$nbretableau = $this->input->post('nbretableau');
		$putableau = $this->input->post('putableau');
		$totaltableau = $this->input->post('totaltableau');
		$reftableau = 3;
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'soin'=>$sointableau,
				'dent'=>$denttableau,
				'nbdent'=>$nbretableau,
				'pu'=>$putableau,
				'total'=>$totaltableau,
				'reftableau'=>$reftableau
			);
		if($sointableau != "" && $denttableau != "" && $nbretableau != "" && $putableau != "" && $totaltableau != "" ){
			$this->MDn->insertDevisTableau($data);	
		}
	}
	
	function devistableau3(){
		$id_clinique = $this->input->post('id_clinique');
		$id_patient = $this->input->post('id_patient');
		$sointableaux = $this->input->post('sointableaux');
		$denttableaux = $this->input->post('denttableaux');
		$nbretableaux = $this->input->post('nbretableaux');
		$putableaux = $this->input->post('putableaux');
		$totaltableaux = $this->input->post('totaltableaux');
		$reftableau = 4;
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'soin'=>$sointableaux,
				'dent'=>$denttableaux,
				'nbdent'=>$nbretableaux,
				'pu'=>$putableaux,
				'total'=>$totaltableaux,
				'reftableau'=>$reftableau
			);
		if($sointableaux != "" && $denttableaux != "" && $nbretableaux != "" && $putableaux != "" && $totaltableaux != "" ){
			$this->MDn->insertDevisTableau($data);	
		}
	}
	
	function envoiReponseClinic(){
		$id_patient = $this->input->post('id_patient');
		$id_clinique = $this->input->post('id_clinique');
		$txtobjet = $this->input->post('txtobjet');
		$reponsecontent = $this->input->post('reponsecontent');
		$isMsgClinique = 1;
		$date = date('Y-m-d');
		$email = $this->MDn->recupEmailPatient($id_patient);
		$this->envoiMailPatientReponseMsg($email,$id_clinique);
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'objet'=>$txtobjet,
				'contenu'=>$reponsecontent,
				'date_envoi'=>$date,
				'isMsgClinique'=>$isMsgClinique
			);
		$this->MDn->insertMsgClinic($data);
	}
	
	function savecommentPatient(){
		$id_patient = $this->input->post('id_patient');
		$id_clinique = $this->input->post('id_clinique');
		$commentPatient = $this->input->post('commentPatient');
		$date = date('Y-m-d');
		$data = array(
				'id_clinique' => $id_clinique,
				'id_patient'=>$id_patient,
				'comment'=>$commentPatient,
				'date_comment'=>$date
			);
		$this->MDn->insertCommentPatient($data);
	}
	
	function modifMateriel2() {
		$origine1 = $this->input->post('origine1');
		$origine2 = $this->input->post('origine2');
		$origine3 = $this->input->post('origine3');
		$id_clinic = $this->input->post('id_clinic');
		if($id_clinic!=0){
			if($origine1==0 && $origine2 != 0 && $origine3 !=0){
				$origine1 = $origine2;
				$origine2 = $origine3;
				$origine3 =0;
			}
			else if($origine1==0 && $origine2 == 0 && $origine3 !=0){
				$origine1 = $origine3;
				$origine2 = 0;
				$origine3 =0;
			}
			else if($origine1!=0 && $origine2 == 0 && $origine3 !=0){
				$origine2 = $origine3;
				$origine3 =0;
			}
			else if($origine1==0 && $origine2 == !0 && $origine3 ==0){
				$origine1 = $origine2;
				$origine2 = 0;
			}
			$result = $this->MDn->updateMaterielClinic2($origine1, $origine2, $origine3, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifBlocClinique () {
		//nom_clinique : $nom_clinique, prothese : $prothese, scanner : $scanner, surface: $surface, fauteuil : $fauteuil, id_clinic : $id_clinic_hidden},				
		$nom_clinique = $this->input->post('nom_clinique');
		$prothese = $this->input->post('prothese');
		$scanner = $this->input->post('scanner');
		$surface = $this->input->post('surface');
		$fauteuil = $this->input->post('fauteuil');
		$id_clinic = $this->input->post('id_clinic');
                $presentationFr = $this->input->post('presentationFr');
                $presentationEn = $this->input->post('presentationEn');
                $presentationDe = $this->input->post('presentationDe');
		if($nom_clinique !=""){
			$result = $this->MDn->updateBlocClinic($nom_clinique, $prothese, $scanner, $surface, $fauteuil, $id_clinic,$presentationFr,$presentationEn,$presentationDe);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifBlocSiteweb () {
		$siteweb = $this->input->post('siteweb');
		$lienretour = $this->input->post('lienretour');;
		$id_clinic = $this->input->post('id_clinic');
		if($siteweb !=""){
			$result = $this->MDn->updateBlocSiteweb($siteweb, $lienretour, $id_clinic);
			echo 1;
		}
		else {
			echo 0;
		}
	}
	
	function modifOrganisation () {			
		$accompagnatrice = $this->input->post('accompagnatrice');
		$transfert = $this->input->post('transfert');
		$reservation = $this->input->post('reservation');
		$reservationBillet = $this->input->post('reservationBillet');
		$id_clinique = $this->input->post('id_clinic');
		if($id_clinique !=""){
			if($this->MDn->verifCG($id_clinique)==1){
				$result = $this->MDn->updateBlocOrganisation($accompagnatrice, $transfert, $reservation, $reservationBillet, $id_clinique); 
			}
			else{
				$data = array(
				'accompagnatrice' => $accompagnatrice ,
				'transfert' => $transfert ,
				'reservation' => $reservation ,
				'reservationBillet' => $reservationBillet ,
				'id_clinique' => $id_clinique
				);
				$this->MDn->insertConditionEtGarantie($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}

	function modifBlocGarantie () {			
		$passeportdentaire = $this->input->post('passeportdentaire');
		$implant = $this->input->post('implant');
		$prothese = $this->input->post('prothese');
		$inlay_Onlay = $this->input->post('inlay_Onlay');
		$id_clinique = $this->input->post('id_clinic');
		if($id_clinique !=""){
			if($this->MDn->verifCG($id_clinique)==1){
				$result = $this->MDn->updateBlocGarantie($passeportdentaire, $implant, $prothese, $inlay_Onlay, $id_clinique); 
			}
			else{
				$data = array(
				'passeportdentaire' => $passeportdentaire ,
				'implant' => $implant ,
				'prothese' => $prothese ,
				'inlay_Onlay' => $inlay_Onlay ,
				'id_clinique' => $id_clinique
				);
				$this->MDn->insertConditionEtGarantie($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}

	function modifBlocReglement () {			
		$cartecredit = $this->input->post('cartecredit');
		$monnaie = $this->input->post('monnaie');
		$id_clinique = $this->input->post('id_clinic');
		if($id_clinique !=""){
			if($this->MDn->verifCG($id_clinique)==1){
				$result = $this->MDn->updateBlocReglement($cartecredit, $monnaie, $id_clinique); 
			}
			else{
				$data = array(
				'cartecredit' => $cartecredit ,
				'monnaie' => $monnaie ,
				'id_clinique' => $id_clinique
				);
				$this->MDn->insertConditionEtGarantie($data);
			}
			echo 1;
		}else{
			echo 0;
		}
	}

	function listDentiste (){
		$id_clinique = intval($this->input->get('id'));
		$data['result']= $this->MDn->getAllDentiste($id_clinique);
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/listDentiste', $data);
		$this->load->view('pagedn/footer');	
	}

	function condition (){
		$this->load->view('pagedn/header');	
		$this->load->view('pagedn/condition');	
		$this->load->view('pagedn/footer');	
	}

	function verifemail2() {
		$email = $this->input->post('email');
		$result = $this->MDn->verifEmailClinic($email);
		if ($result==1){
			echo "tsok";
		}
		else {
			$result = $this->MDn->verifEmailPatient($email);
			if ($result==1){
				echo "tsok";
			}
			else {
				echo "ok";
			}
		}
	}
	
	
	
	function verifCompteClinic() {
		$email = $this->input->post('email');
		$mdp = $this->input->post('mdp');
		$result = $this->MDn->verifCompteClinic($email,$mdp);
		if ($result!=0){
			echo $result;	
		   $newdata = array(
			   'idemail'  => $email,
			   'logged_in' => 'clinic',
			    'idid' => $result
		   );    
		    $_SESSION['idemail'] = $email;	
		    $_SESSION['logged_in'] = 'clinic';	   
		    $_SESSION['idid'] = $result;   
		   $this->session->set_userdata($newdata);	
		}
		else {
			echo "ts ok";
		}
	}
	
	function verifCompteClinicEnter() {
                $this->form_validation->set_rules('email','"Adresse mail"','trim|required|xss_clean|valid_email');
                $this->form_validation->set_rules('password_clinic','"Mot de passe"','trim|required|xss_clean');
                if($this->form_validation->run()==FALSE){
                    $this->load->view('accueil');
                } else {
		$email = $this->input->post('email');
		$mdp = $this->input->post('password_clinic');
		$result = $this->MDn->verifCompteClinic($email,$mdp);
		$isInfoUpdate = $this->MDn->verifInfoClinic($email,$mdp);
		if ($result!=0){
			//echo $result;
		   $newdata = array(
			   'idemail'  => $email,
			   'logged_in' => 'clinic',
			   'idid' => $result
		  );    
		   $_SESSION['idemail'] = $email;	
		   $_SESSION['logged_in'] = 'clinic';	   
		   $_SESSION['idid'] = $result;   
		/*if($isInfoUpdate == 1){
			echo '<script>document.location = "updateC1affiche?id='.$result.'";</script>';
		}else{
			echo '<script>document.location = "updateC1?id='.$result.'";</script>';
		}*/
			//echo '<script>document.location = "updateC1?id='.$result.'";</script>';
                   echo '<script>document.location="espace_clinic";</script>';
		   $this->session->set_userdata($newdata);	
		}
		else {
			//echo "ts ok";
			echo '<script>alert("Erreur login ou mot de passe"); document.location = "../index.php";</script>';
			//header("location:updateC1?id=1");
			//$this->load->view('pagedn/header');	
			//$this->load->view('pagedn/ajoutC1');	
			//$this->load->view('pagedn/footer');	
		}
           }
        
    }
	
	function detail (){
		$id_clinic = $this->input->get('id');
		$id_patient = $this->input->get('id_patient');
		$data['id_patient'] = $id_patient;
		$data['result'] = $this->MDn->getInfoClinicId($id_clinic);
		$data['dentiste'] = $this->MDn->getAllDentiste($id_clinic);
		$data['prixSoins'] = $this->MDn->getPrixSoinsMisAvant($id_clinic);
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/recupClinic', $data);	
		$this->load->view('pagedn/footer');	
	}
	
	function detailComment (){
		$id_clinic = $this->input->get('id');
		$id_patient = $this->input->get('id_patient');
		$data['id_patient'] = $id_patient;
		$data['result'] = $this->MDn->getInfoClinicId($id_clinic);
		$data['dentiste'] = $this->MDn->getAllDentiste($id_clinic);
		$data['prixSoins'] = $this->MDn->getPrixSoinsMisAvant($id_clinic);
		$this->load->view('pagedn/header');
		$this->load->view('pagedn/recupCommentClinic', $data);	
		$this->load->view('pagedn/footer');	
	}
	
	function modifDentiste (){
		
		if (isset($_POST['submit']))
		{				
			$this->load->library('upload');  
			$id_dentiste = $this->input->post('id_dentiste');
			$id_clinique = $this->input->post('id_clinique');
			$photo_dentistehidden = $this->input->post('photo_dentistehidden');
			$nbk = $this->input->post('nbk');
			$langue1 = "";$langue2 = "";$langue3 = "";
			if (isset($_POST['langue1'])) {
				$langue1 = $this->input->post('langue1');
			}
			if (isset($_POST['langue2'])) {
				$langue1 = $this->input->post('langue2');
			}
			if (isset($_POST['langue3'])) {
				$langue3 = $this->input->post('langue3');
			}
			/*
			$langue1 = $this->input->post('langue1');
			$langue2 = $this->input->post('langue2');
			$langue3 = $this->input->post('langue3');
			*/
			
			
			for ($i=1; $i<=$nbk; $i++){
				$id_expProf = $this->input->post('id_expProf_'.$i);
				$expvalue = $this->input->post('expvalue_'.$i);
				$annee2_exp = $this->input->post('annee2_exp'.$i);
				$annee_exp = $this->input->post('annee_exp'.$i);
				$data = array(
					'id_expProf' => $id_expProf,
					'exp' => $expvalue,
					'annee1' => $annee_exp,
					'annee2'=>$annee2_exp
				);
				$this->MDn->updateExpDentiste($data);	
			}
			
			//langue1
			$data = array(
				'id_dentiste' => $id_dentiste,
				'id_clinique' => $id_clinique,
				'photo_dentiste' => $photo_dentistehidden,
				'nom_dentiste'=>$this->input->post('nom_dentiste'),
				'fonction_dentiste'=>$this->input->post('fonction_dentiste'),
				'langue1'=> $langue1,
				'langue2'=> $langue2,
				'langue3'=> $langue3
			);
			$this->MDn->updateDentiste($data);	
			echo "<script>alert('enregistré')</script>";
			$data['result']= $this->MDn->getAllDentiste($id_clinique);
			//$data['isc'] = 1;
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutDentiste');
		}
		else {
			//$data['isc'] = 0;
			$id_clinique = $this->input->get('id');
			$data['result']= $this->MDn->getAllDentiste($id_clinique);
			$this->load->vars($data);
			$this->load->view('pagedn/ajoutDentiste');
		}	
		
	}
	
	function ajouteExp () {
		$id_dentiste = intval($this->input->post('id_dentiste'));
		$annee1 = $this->input->post('annee1');
		$annee2 = $this->input->post('annee2');
		$exp = $this->input->post('exp');
		if ($id_dentiste != 0) {
			$data = array(
				'annee1' => $annee1,
				'annee2' => $annee2 ,
				'exp' => $exp,
				'id_dentiste' => $id_dentiste
			);
			$this->MDn->insertExp($data);
		}	
	}
	
	function ajouteLangueDentiste () {
		$id_dentiste = intval($this->input->post('id_dentiste'));
		$langue = $this->input->post('langue');
		$pos = $this->input->post('pos');
		$this->MDn->updateLangueDentiste($id_dentiste, $langue, $pos);
	}
	
	
	function supprimerExp (){
		$id_exp = intval($this->input->get('id'));
		$id_dentiste = intval($this->input->get('idd'));
		$this->MDn->deleteExp($id_exp);
		$data['result'] = $this->MDn->getDentisteToModif($id_dentiste);
		$this->load->view('pagedn/modifDentiste', $data);			
	}
	
	function supprimerLangue (){
		$pos = intval($this->input->get('pos'));
		$id_dentiste = intval($this->input->get('idd'));
		$this->MDn->updateLangueDentiste($id_dentiste, "", $pos);
		$dentiste = $this->MDn->getDentisteToModif($id_dentiste);
		foreach ($dentiste as $key => $list){	
			$langue1 = $list["langue1"];
			$langue2 = $list["langue2"];
			$langue3 = $list["langue3"];
			if ($langue1=="" && $langue2!="" && $langue3=="") {
				$this->MDn->updateLangueDentiste($id_dentiste, $langue2, 1);
				$this->MDn->updateLangueDentiste($id_dentiste, "", 2);
			}
			else if ($langue1=="" && $langue2!="" && $langue3!="") {
				$this->MDn->updateLangueDentiste($id_dentiste, $langue2, 1);
				$this->MDn->updateLangueDentiste($id_dentiste, $langue3, 2);
				$this->MDn->updateLangueDentiste($id_dentiste, "", 3);
			}
			else if ($langue1=="" && $langue2=="" && $langue3!="") {
				$this->MDn->updateLangueDentiste($id_dentiste, $langue3, 1);
				$this->MDn->updateLangueDentiste($id_dentiste, "", 3);
			}
			else if ($langue1!="" && $langue2=="" && $langue3!="") {
				$this->MDn->updateLangueDentiste($id_dentiste, $langue3, 2);
				$this->MDn->updateLangueDentiste($id_dentiste, "", 3);
			}
		}
		$data['result'] = $this->MDn->getDentisteToModif($id_dentiste);
		$this->load->view('pagedn/modifDentiste', $data);			
	}	
	
	function dentisteToModif (){
		$id_dentiste = intval($this->input->get('id'));
		$data['result'] = $this->MDn->getDentisteToModif($id_dentiste);
		$this->load->view('pagedn/modifDentiste', $data);
	}
	
	function prixSoins (){
		$id_clinique = $this->input->get('id');
		if($id_clinique!=0){
			if ($this->MDn->verifPrixSoinsSave($id_clinique) ==0  ){
				$data = array(
					'intitule_soin' => "Intitulé du soin" ,
					'prix_soin' => "0" ,
					'typePrix' => "1",
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertprixSoinsSave($data);
			}
			if ($this->MDn->verifPrixSoinsSaveAutres($id_clinique) ==0  ){
				$data1 = array(
					'intitule_soin' => "Intitulé du soin" ,
					'prix_soin' => "0" ,
					'typePrix' => "0",
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertprixSoinsSave($data1);
			}	
		}	
		$data['result'] = $this->MDn->getprixSoins($id_clinique);
		$this->load->view('pagedn/prixSoins', $data);
	}
	
	function prixSoinsAutres (){
		/*$id_clinique = $this->input->get('id');
		if($id_clinique!=0){
			if ($this->MDn->verifPrixSoinsSaveAutres($id_clinique) ==0  ){
				$data = array(
					'intitule_soin' => "Soin" ,
					'prix_soin' => "0" ,
					'typePrix' => "0",
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertprixSoinsSave($data);
			}
			if ($this->MDn->verifPrixSoinsSave($id_clinique) ==0  ){
				$data1 = array(
					'intitule_soin' => "Soin" ,
					'prix_soin' => "0" ,
					'typePrix' => "1",
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertprixSoinsSave($data1);
			}
		}
		$data['result'] = $this->MDn->getprixSoinsAutres($id_clinique);*/
		$this->load->view('pagedn/prixSoinsAutres', $data);	
	}
	
	
	function prixSoinsAutresBloc1 (){
		$id_clinique = $this->input->get('id');
		$sq = intval($this->input->get('sq'));
		if($id_clinique!=0){
			if ($this->MDn->verifPrixSoinsSaveAutres1($id_clinique, $sq) ==0  ){
				$data = array(
					'intitule_soin' => "Intitulé du soin" ,
					'prix_soin' => "0" ,
					'typePrix' => $sq,
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertprixSoinsSave($data);
			}
		}
		$data['result'] = $this->MDn->getprixSoinsAutres1($id_clinique, $sq);
		$this->load->view('pagedn/prixSoinsAutresBloc1', $data);	
	}
	
	function prixSoinsSave (){
		$id_clinique = $this->input->post('id_clinic');
		$inprixsoins = $this->input->post('inprixsoins');
		$prixsoins = $this->input->post('prixsoins');
		$typePrix = $this->input->post('typePrix');
		$id_prixSoins = $this->input->post('id_prixSoins');
		$butonprixsoins = $this->input->post('butonprixsoins');
		echo $butonprixsoins.":".$id_prixSoins;
		$data = array(
			'id_prixSoins' => $id_prixSoins ,
			'intitule_soin' => $inprixsoins ,
			'prix_soin' => $prixsoins ,
			'typePrix' => $typePrix,
			'id_clinique' => $id_clinique
		);
		if ($typePrix == 1){
			if ($this->MDn->verifPrixSoinsSave($id_clinique) <=3 ){
				if ($id_prixSoins  == 0 && $butonprixsoins == "Oui"){
					$this->MDn->insertprixSoinsSave($data);
				}
				else if ($id_prixSoins  != 0 && $butonprixsoins == "Oui"){
					$this->MDn->updateprixSoinsSave($data);
				}
				else if ($id_prixSoins  != 0 && $butonprixsoins == "Non"){
					echo "<br />Suppr :".$butonprixsoins;
					$this->MDn->deleteprixSoins($id_prixSoins);
				}
			}
		}
		else {
			if ($this->MDn->verifPrixSoinsSaveAutres($id_clinique) <=15 ){
				if ($id_prixSoins  == 0 && $butonprixsoins == "Oui"){
					$this->MDn->insertprixSoinsSave($data);
				}
				else if ($id_prixSoins  != 0 && $butonprixsoins == "Oui"){
					$this->MDn->updateprixSoinsSave($data);
				}
				else if ($id_prixSoins  != 0 && $butonprixsoins == "Non"){
					$this->MDn->deleteprixSoins($id_prixSoins);
				}
			}
		}	
	}
	
	function mdpoublie2() {
		$email = $this->input->post('email');
		$result = $this->MDn->getInfoClinic($email);
		foreach ($result as $key => $list){
			$mdp = $list['mdp'];
			$this->envoiMailMdpOUbilie($email, $mdp);
		}
	}	

	function init (){
		$email = $this->input->get('id');
		$email = $this->base64url_decode($email);
		//echo $email;
		$this->MDn->updateIsValidClinic($email);
		$this->load->view('pagedn/header');	
		$this->load->view('pagedn/ajoutP1');	
		$this->load->view('pagedn/footer');		
		
	}
	
	private function base64url_decode($plainText) {
	    $base64url = strtr($plainText, '-_,', '+/=');
	    $base64 = base64_decode($base64url);
	    return $base64;  
	}
	
	private function envoiMailClinic($email, $id_clinique) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		//header('Content-type: <type>; charset=utf-8');
		$string = $email;
		$string = chunk_split(base64_encode($string), 64, "\n");
		$url = base_url()."index.php/clinic/init?id=$string";
		//echo "<br />".$string;
		//echo "<br />".base64url_decode($string);
		$message = "Bravo vous avez créé votre espace clinique  dentaire.<br /> ";
		//$message = $message."Nous vous invitons à remplir votre fiche en cliquant <a href='$url"."index.php/dn/updateC1?id=$id_clinique'>ici</a>.";
		$message = $message."Nous vous invitons à remplir votre fiche en cliquant <a href='$url'>ici</a>.";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('aimejules1@gmail.com'); 
		$this->email->subject('Confirmation inscription');
		$this->email->message($message);
		$this->email->send();
	}	

	private function envoiMailMdpOUbilie($email, $mdp) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$message = "Votre mot de passe est $mdp<br /> ";
		$message = $message."";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Recuperation mot de passe');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}	
	
	private function envoiMailDevis($email,$id_patient) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$nomPatient = $this->MDn->getNomInfoPatientId($id_patient);
		$id_pays = $this->MDn->getIpPaysPatientId($id_patient);
		$nomPays =  $this->MDn->selectNomPays($id_pays);
		$message = "Bonjour, <br />vous avez reçu une demande de devis de ".$nomPatient.", originaire de ".$nomPays.".<br />Veuillez consulter votre <a href='$url'>espace clinique</a> pour la consulter.<br />Merci";
		$message = $message."";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Demande de devis');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}	

	private function envoiMailReponseDD($email,$id_clinique) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$urlclinique = base_url()."index.php/clinic/detail?id=".$id_clinique;
		$message = "Bonjour, <br />vous avez reçu un devis de la clinique <a href='$urlclinique'>espace clinique</a> .<br />";
		$message = $message."Suivez ce <a href='$url'>lien</a> pour le consulter";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Réponse d\'une demande de devis');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}
	
	private function envoiMailClinicReponseMsg($email) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url();
		$message = "Bonjour, <br />vous avez reçu un message de la part d'un patient.<br />";
		$message = $message."Suivez ce <a href='$url'>lien</a> pour l'examiner";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Message d\'un patient');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}
	
	private function envoiMailPatientReponseMsg($email,$id_clinique) {
		$this->load->helper('email');
		$this->load->library('email');
		$this->email->set_mailtype("html");
		header('Content-type: <type>; charset=utf-8');
		$url = base_url()."index.php/clinic/detail?id=".$id_clinique;
		$message = "Bonjour, <br />vous avez reçu un message de la clinique <a href='$url'>espace clinique</a> .<br />Merci";
		$message = $message."";
		$this->email->from('centredentaire@centredentaire.net', 'Centre dentaire');
		$this->email->to($email);
		$this->email->cc('domoinaalivelo@gmail.com');  
		$this->email->subject('Message d\'une clinique');
		$this->email->message($message);
		$this->email->send();
		//echo "envoie email ".$email."<br />";
	}
	
	function modepaiement(){
		$data = array();		
		$id_clinique = $this->input->get('id');
		if($id_clinique!=0){
			if ($this->MDn->verifModePaiementSave($id_clinique) ==0  ){
				$data = array(
					'type_paiement' => "Euro" ,
					'id_clinique' => $id_clinique
				);
				$this->MDn->insertmodePaiementSave($data);
			}	
		}	
		$data["result"] = $this->MDn->getAllmodepaiement($id_clinique);
		$this->load->vars($data);
		$this->load->view('pagedn/modepaiement');
	}
	
	function modepaiementSave(){
		$id_patient = $this->input->post('id_patient');
		$butonLangage = $this->input->post('butonLangage');
		$Langage = $this->input->post('langage');
		$id_Langage = $this->input->post('id_Langage');
		//butonLangage : butonLangage, langage : Langage, id_patient : id_clinic, id_Langage : id_Langage
		echo $butonLangage.":".$Langage.":".$id_Langage.":".$id_patient;
		if ($butonLangage == "Oui"){
			if($Langage != "Selectionner"){
				if($id_Langage!=0){
					$this->MDn->updatemodepaiement($id_Langage, $Langage);
				}
				else {
					$data = array(
						'type_paiement' => $Langage ,
						'id_clinique' => $id_patient
					);
					$this->MDn->insertmodePaiementSave($data);
				}
			}
		}
		else if ($butonLangage == "Non"){
			$this->MDn->deletemodepaiement($id_Langage, $Langage);
		}
	}
	
	private function array_delete_key($array,$search) {
		$temp = array();
		foreach($array as $key => $value) {
		 if($key!=$search) $temp[$key] = $value;
		}
		return $temp;
	}
	
	function paginateClinic(){
		$id_patient = $this->input->post('id_patient');
		$listclinic = $this->input->post('listclinic');
		$id_clinic = $listclinic;
		$value = $this->MDn->verifPaginate($id_patient);
		if ($value==0){
			$data = array(
					'id_patient' => $id_patient ,
					'listclinic' => $listclinic
			);
			$this->MDn->insertpaginateClinic($data);
		}
		else {
			$ancien = $this->MDn->recupPaginate($value);
			//$listclinic = $listclinic.",".$ancien;
			//$this->MDn->updatepaginateClinic($value, $listclinic);
			//$new = $this->MDn->recupPaginate($value);
			$input = explode(",", $ancien);
			$result = array_unique($input);
			//print_r($result);
			if (in_array($id_clinic, $result, true)){
				unset($result[array_search($id_clinic, $result)]);
				$listclinic = implode(",", $result);
				$this->MDn->updatepaginateClinic($value, $listclinic);
				//echo "misy";
				//print_r($result);
			}
			else {
				$listclinic = implode(",", $result);
				$listclinic = $listclinic.",".$id_clinic;
				//echo "ts misy";
				$this->MDn->updatepaginateClinic($value, $listclinic);
			}
			print_r($result);
		}
	}
	
	function paginateClinicSelect(){
		$id_patient = $this->input->post('id_patient');
		$listclinic = $this->input->post('listclinic');
		$value = $this->MDn->verifPaginate($id_patient);
		if ($value!=0){
			$this->MDn->updatepaginateClinic($value, $listclinic);
			$new = $this->MDn->recupPaginate($value);
		}
	}
	
	function demandeDevisEnvois(){
		$id_patient = $this->input->get('id');
		$value = $this->MDn->verifPaginate($id_patient);
		if ($value!=0){
			$ancien = $this->MDn->recupPaginate($value);
			$listclinic = $listclinic.",".$ancien;
			$this->MDn->updatepaginateClinic($value, $listclinic);
			$new = $this->MDn->recupPaginate($value);
			$input = explode(",", $new);
			$result = array_unique($input);
			foreach ($result as $id_clinique) {
				if ($id_clinique !=0 || $id_clinique !=""){
					$ids_clinique = $ids_clinique.",".$id_clinique;
					if ($this->MDn->verifdevisRecus($id_patient, $id_clinique)==0){
						$email = $this->MDn->recupEmailClinic($id_clinique);
						$this->envoiMailDevis($email,$id_patient);
						$date = date('Y-m-d');
						$data = array(
							'id_patient' => $id_patient ,
							'id_clinique' => $id_clinique,
							'date_envoi' => $date
						);
						$this->MDn->insertDevisRecu($data);
					}
				}
			}
			$this->MDn->deletepaginateClinic($id_patient);
			$data['result']= $ids_clinique;
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/listDevisClinic', $data);
			$this->load->view('pagedn/footer');
		}else{
			$data['result']= "";
			$this->load->view('pagedn/header');
			$this->load->view('pagedn/listDevisClinic', $data);
			$this->load->view('pagedn/footer');
		}
	}
	
	function deletePaginate(){
		$id_patient = $this->input->post('id_patient');
		$this->MDn->deletepaginateClinic($id_patient);
	}
	
	function deleteGalerie(){
		$id_galerieImage = $this->input->post('id_galerieImage');
		$this->MDn->deleteGalerie($id_galerieImage);
	}
	
	
	function deleteSoin(){
		$id_photoSoin = $this->input->post('id_photoSoin');
		$this->MDn->deleteSoin($id_photoSoin);
	}
	function recherchemulti (){
		$this->load->library("pagination");
		$p = intval($this->input->get('p'));
		$v = intval($this->input->get('v'));
		$config = array();
		$config["base_url"] = base_url() . "index.php/clinic/recherchemulti";
		$config["total_rows"] = $this->MDn->record_count_getMultiClinicFetch($p, $v);
		$config["per_page"] = $this->per_page;
		$config["uri_segment"] = $this->uri_segment;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$data['result']= $this->MDn->getMultiClinicFetch($config["per_page"], $page, $p, $v);
		$data["links"] = $this->pagination->create_links();
                $this->load->view('clinic/vue_listclinic',$data);
	}
        function espace_clinic(){
            $this->load->view('clinic/vue_espace_clinic');
        }
}
?>